
function TreeColSelectRender(tree,displayDegree,padWidth,radioName){
	this.tree = tree ;
	this.id = TreeColSelectRender.defaultInstanceName ;
	this.defaultValue = null ;
	this.selectSize = 10 ;
	this.emptyLabel = '----' ;
	this.nullable = false ;
	return this ;
}

TreeColSelectRender.defaultInstanceName = "TCSR";

TreeColSelectRender.prototype.bindById = function () {
    var key = "TreeColSelectRender." + this.id;
    window[key] = this;
}

function findTreeColSelectRenderInstance() {
    var key = "TreeColSelectRender." + TreeColSelectRender.defaultInstanceName;
    return window[key];
}


TreeColSelectRender.prototype.refresh = function(){
//	this.tree.init();
	var html = this.getTreeHTML() ;
//	document.write(html);
	var div = document.getElementById("DivTree");
	div.innerHTML = html ;
}

TreeColSelectRender.prototype.render = function(){
	if(!this.tree.neatened){
		this.tree.init();
	}
	var html = this.getTreeHTML() ;
	document.write("<div id='DivTree' style='width:99%;height:100%;text-align:left;vertical-align:top;'></div>");
	var div = document.getElementById("DivTree");
	div.innerHTML = html ;
}

TreeColSelectRender.prototype.getTreeHTML = function(){
	var degree = this.tree.getDegree();
	var width = Math.round(100*1.0 / degree) ;
	var buffer = new StringBuffer();
	var list = this.getFamilyNodes();
	
	buffer.append("<table class=''>");
	buffer.append("<tr>");

	for(var i=0;i<degree;i++){
		buffer.append("<td id='TCSR_").append(i).append("'>");		
		if(list != null && list.size() > i){
			var id = list.get(i);
			var node = this.tree.findNodeById(id) ;
			var parent = node.parentNode ;
			buffer.append(this.getTreeNodeHTML(parent,list));
		}

		buffer.append("</td>");
	}

	buffer.append("</tr>");
	buffer.append("</table>");
	
	return buffer.toString();
}

TreeColSelectRender.prototype.getTreeNodeHTMLById = function(id){
	var node = this.tree.findNodeById(id);
	return this.getTreeNodeHTML(node,this.getFamilyNodes());
}

TreeColSelectRender.prototype.getTreeNodeHTML = function(node,matrix){
	var list = node.children;
	if(list.isEmpty()){
		return "";
	}
	var buffer = new StringBuffer();
	buffer.append("<select name='button_");
	buffer.append(node.rank);
	buffer.append("' size='").append(this.selectSize).append("'");
	buffer.append(" id='TCSR_SELECT_").append(node.rank).append("'");
	buffer.append(" onchange=\"nextOrReturn(this,").append(node.rank).append(")\"");
	buffer.append(">");

	if(this.nullable){
		buffer.append("<option value=''>").append(this.emptyLabel).append("</option>");
	}

	for(var i=0;i<list.size();i++){
		node = list.get(i) ;
		buffer.append("<option value='").append(node.id).append("'");
		if(matrix.contains(node.id) >= 0){
			buffer.append(" selected");
		}
		buffer.append(">");
		buffer.append(node.name);
		buffer.append("</option>");
	}

	buffer.append("</select>");

	return buffer.toString();
}

TreeColSelectRender.prototype.getFamilyNodes = function(){
	if(this.defaultValue == null){
		return null ;
	}
	var list = new ArrayList();
	var id = this.defaultValue ;
	while(true){
		var node = this.tree.findNodeById(id);
		if(node == null){
			break ;
		}
		list.add(node.id);
		id = node.parentId ;
		if(node.parentId == this.tree.root.id){
			break ;
		}
	}
	if((list == null || list.isEmpty()) && this.tree.root.hasChild()){
		list = new ArrayList();
		list.add(this.tree.root.getChildAt(0).id);
	}
	if(list != null){
		list.reverse() ;
	}
	return list ;
}

function changeTreeView(id){
	var viewRender = findTreeColSelectRenderInstance();
	var node = viewRender.tree.findNodeById(id);
	if(node.sequence >= node.shadowIndex){
		return ;
	}
	viewRender.opens[node.sequence] = !viewRender.opens[node.sequence] ;
	var status = viewRender.opens[node.sequence] ;
	var size = viewRender.tree.nodes.size();
	for(var i=(node.sequence+1);i<=node.shadowIndex;i++){
		if(i >= size){
			break ;
		}
		viewRender.displays[i] = status ;
		viewRender.opens[i] = status ;
	}
	viewRender.refresh();
}

function nextOrReturn(me,rank){
	if(me.value == ""){
		clearSprings(rank+1);
	}else{
		clearSprings(rank+2);
	}
	var col = document.getElementById("TCSR_"+(rank+1));
	if(col == null){
		try{
			var index = me.options.selectedIndex ;
			var option = me.options[index] ;
			parent.returnElement(option.value , getAllSelectedNames());
		}catch(e){
			alert(e);
			// 
		}
		return ;
	}
	var render = findTreeColSelectRenderInstance();
	var value = me.options[me.options.selectedIndex].value ;
	if(value != null && value != ""){
		var html = render.getTreeNodeHTMLById(value);
			if(html == ""){
				try{
				var index = me.options.selectedIndex ;
				var option = me.options[index] ;
				parent.returnElement(option.value , getAllSelectedNames());
			}catch(e){
				alert(e);
				// 
			}
		}

		// alert(html);

		col.innerHTML = html ;
	}
}

function getAllSelectedNames(){
	var begin = 0 ;

	var name = "";
	var first = true ;
	while(true){
		var node = document.getElementById("TCSR_SELECT_"+(begin));
		if(node == null){
			break ;
		}
		if(node.options.selectedIndex >= 0){
			if(!first){
				name += "->" ;
			}
			name += node.options[node.options.selectedIndex].text ;
			first = false ;
		}
		begin++ ;
	}
	return name ;
}

function clearSprings(rank){
	var begin = rank ;
	while(true){
		var col = document.getElementById("TCSR_"+(begin));
		if(col == null){
			return ;
		}
		col.innerHTML = "";
		begin++ ;
	}
}